using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using usm.SolDev.DataBase.DataObjects;
using usm.SolDev.DataBase.DataObjects.Experiment;

namespace usm.SolDev.UI.Charting
{
    public partial class ChartOptionsForm : usm.SolDev.UI.ToolWindow
    {

        #region Refresh chart delegate
        public delegate void RefreshChartDelegate(object sender, ChartDataObject chartData);
        public event RefreshChartDelegate RefreshChart;
        #endregion

        #region private data
        /// <summary>
        ///     The assigned chart data
        /// </summary>
        private ChartDataObject chartData;
        #endregion

        #region Constructors
        /// <summary>
        ///     Default constructor
        /// </summary>
        public ChartOptionsForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Public Properties]
        /// <summary>
        ///     Assign a chart whose options will be reflected and managed
        /// </summary>
        public ChartDataObject ChartData 
        {
            get { return chartData; }
            set
            {
                chartData = value;
                cbEntities.Items.Clear();
                cbEntities.Text = String.Empty;
                cbExperiment.Items.Clear();
                cbExperiment.Text = "";


                #region Reload chart options data
                if (chartData != null)
                {
                    if (chartData.ParsedResults != null && chartData.ParsedResults.Entities != null)
                    {
                        foreach (Entity entity in chartData.ParsedResults.Entities)
                        {
                            cbEntities.Items.Add(entity);
                        }

                        if (cbEntities.Items.Count > 0)
                        {
                            cbEntities.SelectedItem = cbEntities.Items[0];
                        }

                        for (int i = 0; i < System.Enum.GetValues(typeof(ChartType)).Length; i++ )
                        {
                            cbChartType.Items.Add(ChartDataObject.ChartTypeDescription[i]);
                        }

                        if (cbChartType.Items.Count > 0)
                        {
                            cbChartType.SelectedItem = cbChartType.Items[0];
                        }

                        for (int i = 0; i < chartData.ParsedResults.Experiments.Count; i++)
                        {
                            cbExperiment.Items.Add(String.Format("{0}", i));
                        }

                        if (cbExperiment.Items.Count > 0)
                        {
                            cbExperiment.SelectedItem = cbExperiment.Items[0];
                        }


                    }
                }
                #endregion
            }
        }
        #endregion

        #region Form events handler
        /// <summary>
        ///     When user presses refresh button, call the event subscriber
        /// and perform refresh of chart
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if ((RefreshChart != null) && (chartData != null))
            {
                if (cbEntities.SelectedItem == null )
                {
                    MessageBox.Show("Please select an entity to analyse");
                    return;
                }

                if (cbExperiment.SelectedItem == null)
                {
                    MessageBox.Show("Please select an experiment number to analyse");
                    return;
                }

                chartData.ActiveEntity = (Entity)cbEntities.SelectedItem;
                chartData.Experiment = int.Parse(cbExperiment.SelectedItem.ToString());
                RefreshChart(sender, chartData);
            }
        }
        #endregion
    }
}